package com.aniu.smslisten.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.telephony.SmsMessage;

import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;

import com.aniu.smslisten.util.LogToFile;
import com.aniu.smslisten.util.NotificationUtil;
import com.aniu.smslisten.weixin.PushWorker;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * 短信监听器，震动及推送通知
 */
public class SmsReceiver extends BroadcastReceiver {
    private static final String TAG = "SmsReceiver";
    private ArrayList<String> specificContents;

    @Override
    public void onReceive(Context context, Intent intent) {
        LogToFile.log(context, TAG, "SmsReceiver onReceive called");
        // 从SharedPreferences读取specificContents
        SharedPreferences sharedPreferences = context.getSharedPreferences("SmsListenPrefs", Context.MODE_PRIVATE);
        specificContents = new ArrayList<>(sharedPreferences.getStringSet("specificContents", new HashSet<String>() {{
            add("立即驶离");
        }}));

        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus != null) {
                for (Object pdu : pdus) {
                    SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
                    String messageBody = smsMessage.getMessageBody();
                    LogToFile.log(context, TAG, "Received SMS: " + messageBody);

                    for (String content : specificContents) {
                        if (messageBody.contains(content)) {
                            pushMessage(context, messageBody);
                            vibratePhone(context);
                            NotificationUtil.showNotification(context, "SMS Alert", messageBody, 1);
                            break;
                        }
                    }
                }
            }
        }
    }

    private void pushMessage(Context context, String message) {
        Data inputData = new Data.Builder()
                .putString("message", message)
                .build();

        OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(PushWorker.class)
                .setInputData(inputData)
                .build();

        WorkManager.getInstance(context).enqueue(workRequest);
    }

    private void vibratePhone(Context context) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        if (vibrator != null && vibrator.hasVibrator()) {
            long[] vibrationPattern = new long[]{0, 1000, 1000}; // 振动1秒，停顿1秒
            VibrationEffect effect = VibrationEffect.createWaveform(vibrationPattern, 0); // 重复模式
            vibrator.vibrate(effect); // 使用VibrationEffect
        }
    }
}